package com.ittest.service.store.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ittest.domain.store.Product;
import com.ittest.factory.MapperFactory;
import com.ittest.mapper.store.ProductMapper;
import com.ittest.service.store.ProductService;
import com.ittest.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.UUID;

public class ProductServiceImpl implements ProductService {
    @Override
    public void add(Product product) {
        SqlSession sqlSession = null;
        try {
            //获取sqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Mapper代理对象
            ProductMapper productMapper = MapperFactory.getMapper(sqlSession, ProductMapper.class);
            String id = UUID.randomUUID().toString();
            product.setId(id);
            productMapper.add(product);
            //提交事件
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            //回滚事务
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            //释放资源
            if (sqlSession!=null) {
                TransactionUtil.close(sqlSession);
            }
        }
    }

    @Override
    public void update(Product product) {
        SqlSession sqlSession = null;
        try {
            //获取sqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Mapper代理对象
            ProductMapper productMapper = MapperFactory.getMapper(sqlSession, ProductMapper.class);
            productMapper.update(product);
            //提交事件
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            //回滚事务
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            //释放资源
            if (sqlSession!=null) {
                TransactionUtil.close(sqlSession);
            }
        }
    }

    @Override
    public void delete(String id) {
        SqlSession sqlSession = null;
        try {
            //获取sqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Mapper代理对象
            ProductMapper productMapper = MapperFactory.getMapper(sqlSession, ProductMapper.class);
            //处理数据
            String[] ids = id.split(",");
            productMapper.delete(ids);
            //提交事件
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            //回滚事务
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            //释放资源
            if (sqlSession!=null) {
                TransactionUtil.close(sqlSession);
            }
        }
    }

    @Override
    public PageInfo findAll(String sname, int page, int size) {
        SqlSession sqlSession = null;
        PageInfo pageInfo = null;
        try {
            //获取sqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Mapper代理对象
            ProductMapper productMapper = MapperFactory.getMapper(sqlSession, ProductMapper.class);
            //分页
            PageHelper.startPage(page,size);
            List<Product> list = productMapper.findAll(sname);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (Product p : list) {
                String format = sdf.format(p.getDepartureTime());
                p.setDepartureTimeStr(format);
                Integer status = p.getProductStatus();
                if (status == 1){
                    p.setProductStatusStr("已开启");
                }else if (status==0){
                    p.setProductStatusStr("已屏蔽");
                }
            }
            //封装pageInfo
            pageInfo= new PageInfo(list);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //释放资源
            if (sqlSession!=null) {
                TransactionUtil.close(sqlSession);
            }
        }
        return pageInfo;
    }

    @Override
    public Product findById(String id) {

        SqlSession sqlSession = null;
        Product product = null;
        try {
            //获取sqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Mapper代理对象
            ProductMapper productMapper = MapperFactory.getMapper(sqlSession, ProductMapper.class);
            product = productMapper.findById(id);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = sdf.format(product.getDepartureTime());
            product.setDepartureTimeStr(format);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //释放资源
            if (sqlSession!=null) {
                TransactionUtil.close(sqlSession);
            }
        }
        return product;
    }

    @Override
    public void open(String id) {
        SqlSession sqlSession = null;
        try {
            //获取sqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Mapper代理对象
            ProductMapper productMapper = MapperFactory.getMapper(sqlSession, ProductMapper.class);
            String[] ids = id.split(",");
            productMapper.open(ids);
            //提交事件
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            //回滚事务
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            //释放资源
            if (sqlSession!=null) {
                TransactionUtil.close(sqlSession);
            }
        }
    }

    @Override
    public void close(String id) {
        SqlSession sqlSession = null;
        try {
            //获取sqlSession
            sqlSession = MapperFactory.getSqlSession();
            //获取Mapper代理对象
            ProductMapper productMapper = MapperFactory.getMapper(sqlSession, ProductMapper.class);
            String[] ids = id.split(",");
            productMapper.close(ids);
            //提交事件
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            //回滚事务
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            //释放资源
            if (sqlSession!=null) {
                TransactionUtil.close(sqlSession);
            }
        }
    }
}
